<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>我的收藏夹</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .favorites-list {
            margin-bottom: 30px;
        }

        .favorite-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #eee;
            padding: 12px 0;
        }

        .favorite-item:last-child {
            border-bottom: none;
        }

        .fav-title {
            font-weight: bold;
            color: #2c3e50;
        }

        .fav-url {
            color: #3498db;
            text-decoration: none;
            margin-left: 10px;
            word-break: break-all;
        }

        .fav-url:hover {
            text-decoration: underline;
        }

        .btn-delete {
            background-color: #e74c3c;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 6px 14px;
            cursor: pointer;
            font-size: 14px;
            margin-left: 20px;
        }

        .btn-delete:hover {
            background-color: #c0392b;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: bold;
        }

        input[type="text"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .btn-submit {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        .btn-submit:hover {
            background-color: #45a049;
        }

        .no-data {
            text-align: center;
            color: #888;
            padding: 20px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>我的收藏夹</h1>
    </div>

    <%-- 处理添加和删除收藏 --%>
    <%
        request.setCharacterEncoding("UTF-8");
        List<Map<String, String>> favorites = (List<Map<String, String>>) session.getAttribute("favorites");
        if (favorites == null) {
            favorites = new ArrayList<>();
            session.setAttribute("favorites", favorites);
        }
        String method = request.getMethod();
        String action = request.getParameter("action");
        if ("POST".equalsIgnoreCase(method)) {
            if ("add".equals(action)) {
                String title = request.getParameter("title");
                String url = request.getParameter("url");
                if (title != null && !title.trim().isEmpty() && url != null && !url.trim().isEmpty()) {
                    Map<String, String> item = new HashMap<>();
                    item.put("title", title.trim());
                    item.put("url", url.trim());
                    favorites.add(0, item); // 新收藏置顶
                }
            } else if ("delete".equals(action)) {
                String indexStr = request.getParameter("index");
                try {
                    int idx = Integer.parseInt(indexStr);
                    if (idx >= 0 && idx < favorites.size()) {
                        favorites.remove(idx);
                    }
                } catch (Exception e) {
                }
            }
        }
    %>

    <div class="favorites-list">
        <% if (favorites.isEmpty()) { %>
        <p class="no-data">暂无收藏，快去添加吧！</p>
        <% } else { %>
        <% for (int i = 0; i < favorites.size(); i++) {
            Map<String, String> item = favorites.get(i); %>
        <div class="favorite-item">
            <span class="fav-title"><%= item.get("title") %></span>
            <a class="fav-url" href="<%= item.get("url") %>" target="_blank"><%= item.get("url") %>
            </a>
            <form method="post" style="display:inline;margin:0;">
                <input type="hidden" name="action" value="delete"/>
                <input type="hidden" name="index" value="<%= i %>"/>
                <button type="submit" class="btn-delete">删除</button>
            </form>
        </div>
        <% } %>
        <% } %>
    </div>

    <form method="post" autocomplete="off">
        <input type="hidden" name="action" value="add"/>
        <div class="form-group">
            <label for="title">标题：</label>
            <input type="text" id="title" name="title" maxlength="30" required/>
        </div>
        <div class="form-group">
            <label for="url">网址：</label>
            <input type="text" id="url" name="url" maxlength="200" required placeholder="如：https://www.example.com"/>
        </div>
        <button type="submit" class="btn-submit">添加收藏</button>
    </form>
</div>
</body>
</html>
